// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
import './common/css/base.styl'
import App from './App'
import router from './router'
import VueAMap from 'vue-amap'
import VCharts from 'v-charts'
import jquery from 'jquery'

Vue.use(Vuex);
Vue.use(ElementUI);
Vue.use(VueAMap);
Vue.use(VCharts);
Vue.use(jquery);
Vue.config.productionTip = false;
Vue.prototype.$axios = axios;

// vuex
const store = new Vuex.Store({
  state: {
    isLogin: localStorage.token ? 1 : 0
  },
  mutations: {
    login (state) {
    	state.isLogin = 1;
    }
  }
})

// 地图初始化

VueAMap.initAMapApiLoader({
  key: 'b0656910e9ed2608b21355a3e0f6806a',
  plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor']
});

router.beforeEach((to, from, next) => {	
	console.log('router');
	// 判断登录状态
	if (to.matched.some(record => record.meta.requiresAuth)) {		
		// this route requires auth, check if logged in
		// if not, redirect to login page.
		if (store.state.isLogin) {
			next();
		} else {
			next('/');
		}
	} else {
		next() // 确保一定要调用 next()
	}
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})

